package hfut.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hfut.example.common.BaseContext;
import hfut.example.common.R;
import hfut.example.entity.ShoppingCart;
import hfut.example.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加
     * @param shoppingCart 只包含setmealId / dishId
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户Id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //查询是否已经存在
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart oneCart = shoppingCartService.getOne(queryWrapper);

        if(oneCart!=null){
            //存在则数量加一
            oneCart.setNumber(oneCart.getNumber()+1);
            shoppingCartService.updateById(oneCart);
        }else {
            //不存在,则添加到购物车,数量为一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            oneCart=shoppingCart;
        }

        return R.success(oneCart);
    }

    /**
     * 减少
     * @param shoppingCart 只包含setmealId / dishId
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //设置用户Id
        Long userId = BaseContext.getCurrentId();

        //查询是否已经存在
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        //查看为套餐还是为菜品
        if(dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart oneCart = shoppingCartService.getOne(queryWrapper);

        if(oneCart.getNumber()!=1){
            //数量不为一则减一
            oneCart.setNumber(oneCart.getNumber()-1);
            shoppingCartService.updateById(oneCart);
        }else {
            //数量为一则删除
            shoppingCartService.remove(queryWrapper);
        }

        return R.success(oneCart);
    }

    /**
     * 只需查询当前用户的所有商品即可
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){


        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 删除当前用户的全部菜品即可
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);


        return R.success("清空购物车成功");
    }
}
